package com.woniu.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.woniu.entity.DataRendering;
import com.woniu.entity.EnhancedDataRendering;
import com.woniu.entity.UserInfoEntity;
import com.woniu.entity.ViewDetails;
import com.woniu.service.EnhancedViewDetailsService;
import com.woniu.service.ViewDetailsService;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 查看详情
 * @author: sunkun
 * @date: 2022/9/21 11:09
 * @version 1.0
 */
@RestController
@RequestMapping("/viewDetails")
public class ViewDetailsController {

    @Resource
    ViewDetailsService viewDetailsService;

    @GetMapping("/userInfo")
    public AjaxResult newViewDetails(){
        List<DataRendering> dataRenderings = viewDetailsService.newViewDetails();
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功！",dataRenderings);
    }

    @PostMapping(value = "/saveInfo",produces = "application/json")
    public AjaxResult saveInfoToClientTable(@RequestBody UserInfoEntity userInfo){
        //字符串压缩
//        String s1 = Base64.encodeBase64String(userInfo.getBytes(StandardCharsets.UTF_8));
//        System.out.println(s1);
        //字符串解压
//        String s2 = Base64.decodeBase64(s1);
//        System.out.println(s2);

        System.out.println("userInfo:"+userInfo);//前端将clientId塞进去？？？？？

        //将数据存入数据库中:e.g.存储到客户1中
        //TODO 需要从前端传递客户id
        //把客户信息封装进json中并转为字符串,其key设定为"jsonStr"
        if(viewDetailsService.saveUserInfo(JSON.toJSONString(new HashMap<String,String>(){
            {
                put("jsonStr",userInfo.getIJSONString());
            }
        })  , userInfo.getId())){
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @GetMapping("/findNewViewsDetails/{clientId}")
    public AjaxResult findNewViewsDetails(@PathVariable Integer clientId){
        List<DataRendering> allList = viewDetailsService.findNewViewsDetails(clientId);
        //新增后使用该查询语句查询个人信息
        //List<DataRendering> allList = viewDetailsService.newFindViewsDetails(clientId);
        return new AjaxResult(HttpStatus.SUCCESS,"查询成功",allList);
    }
}

